<template>
	<div class="system-menu-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="960px">
			<el-row :gutter="20">
				<el-col :span="14">
					<el-form ref="addGradeDialogFormRef" :rules="state.rules" :model="state.ruleForm"  label-width="80px">
						<el-form-item label="模板类型">
							<el-select  placeholder="请选择商品属性" class="form-ele-size">
								<el-option :label="item.value" :value="item.id" v-for="item of state.specsList" :key="item.id" /> 
							</el-select>
						</el-form-item>
						<el-form-item label="模板内容">
							<el-input type="textarea" v-model="state.ruleForm.grade" placeholder="请输入模板内容" clearable></el-input>
						</el-form-item>
						<el-form-item >
							<span class="dialog-footer">
								<el-button @click="onCancel"  >取 消</el-button>
								<el-button type="primary" @click="onSubmit"  >{{ state.dialog.submitTxt }}</el-button>
							</span>
						</el-form-item>
					</el-form>
				</el-col>
				<el-col :span="10">
					<el-card class="box-card">
						<el-row>
							<el-col :span="12">${username}会员姓名</el-col>
							<el-col :span="12">${username}会员姓名</el-col>
							<el-col :span="12">${username}会员姓名</el-col>
							<el-col :span="12">${username}会员姓名</el-col>
							<el-col :span="12">${username}会员姓名</el-col>
							<el-col :span="12">${username}会员姓名</el-col>
							<el-col :span="12">${username}会员姓名</el-col>
							<el-col :span="12">${username}会员姓名</el-col>
							<el-col :span="12">${username}会员姓名</el-col>
						</el-row>
					</el-card>
				</el-col>
			</el-row>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="addSourceDialog">
import { levelSave, levelUpdate } from "/@/api/member";
import { reactive, onMounted, ref } from 'vue';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 定义变量内容
const addGradeDialogFormRef = ref();
const state = reactive({
	specsList:[
		{
			value: '衣服',
			id: 229,
		},
		{
			value: '鞋子',
			id: 229,
		}
	],
	ruleForm: {
		id: '',
		name: '', // 等级名称
		experience:'', // 经验值 
		grade: '', // 等级
		discount: '', // 折扣 百分比
		image: '', // 图片
		icon: '', // 图标
		memo: '',
		createTime: '',
		updateTime: '',
		storeId: '',
		del: '',
		show: ''
	},
	rules: {
		name: { required: true, message: '请输入等级名称', trigger: 'blur' },
		experience: { required: true, message: '请输入经验值', trigger: 'blur' },
		grade: { required: true, message: '请输入等级', trigger: 'blur' },
		discount: { required: true, message: '请输入折扣百分比', trigger: 'change' },
		icon: { required: true, message: '请选择图片', trigger: 'change' },
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '新增模板',
		submitTxt: '',
	},
});


// 打开弹窗
const openDialog = (type: string, row?: any) => {
	if (type === 'edit') {
		state.ruleForm = JSON.parse(JSON.stringify(row));
		state.dialog.title = '模板编辑';
		state.dialog.submitTxt = '修 改';
	} else {
		state.ruleForm = {
			id: '',
			name: '', // 等级名称
			experience:'', // 经验值 
			grade: '', // 等级
			discount: '', // 折扣 百分比
			image: '', // 图片
			icon: '', // 图标
			memo: '',
			createTime: '',
			updateTime: '',
			storeId: '',
			del: '',
			show: ''
		},
		state.dialog.title = '新增模板';
		state.dialog.submitTxt = '新 增';
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async () => {
	if(state.dialog.type == 'edit'){
	// 编辑
		let res = await levelUpdate(state.ruleForm);
		console.log(res)
	}else{
		let res = await levelSave(state.ruleForm);
		console.log(res)
	}
	closeDialog(); // 关闭弹窗
	emit('refresh');
	
	// if (state.dialog.type === 'add') {}
	// setBackEndControlRefreshRoutes() // 刷新菜单，未进行后端接口测试
};
// 页面加载时
onMounted(() => {

});

// 暴露变量
defineExpose({
	openDialog,
});
</script>
